package com.ruoyi.zlgl.zlsb.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zlgl.zlsb.domain.ZlglZlsb;
import com.ruoyi.zlgl.zlsb.domain.ZlglZlsbFj;
import com.ruoyi.zlgl.zlsb.mapper.ZlglZlsbMapper;
import com.ruoyi.zlgl.zlsb.service.IZlglZlsbService;
import com.ruoyi.zlgl.zlsb.vo.ZlSqVo;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 专利申报Service业务层处理
 *
 * @author huacai
 * @date 2024-08-15
 */
@Service
public class ZlglZlsbServiceImpl implements IZlglZlsbService {
    @Resource
    private ZlglZlsbMapper zlglZlsbMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询专利申报
     *
     * @param zlsbId 专利申报主键
     * @return 专利申报
     */
    @Override
    public ZlglZlsb selectZlglZlsbByZlsbId(String zlsbId) {
        return zlglZlsbMapper.selectZlglZlsbByZlsbId(zlsbId);
    }

    /**
     * 查询专利申报列表
     *
     * @param zlglZlsb 专利申报
     * @return 专利申报
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<ZlglZlsb> selectZlglZlsbList(ZlglZlsb zlglZlsb) {
        return zlglZlsbMapper.selectZlglZlsbList(zlglZlsb);
    }

    /**
     * 查询专利申报列表 (无数据权限校验)
     * @param zlglZlsb
     * @return
     */
    @Override
    public List<ZlglZlsb> selectZlList(ZlglZlsb zlglZlsb) {
        return zlglZlsbMapper.selectZlList(zlglZlsb);
    }

    /**
     * 新增专利申报
     *
     * @param zlglZlsb 专利申报
     * @return 结果
     */
    @Transactional
    @Override
    public int insertZlglZlsb(ZlglZlsb zlglZlsb) {
        zlglZlsb.setCreateTime(DateUtils.getNowDate());
        zlglZlsb.setIdWhere("存在");
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 插入用户ID
        zlglZlsb.setDeptId(Math.toIntExact(loginUser.getUser().getDeptId()));
        int rows = zlglZlsbMapper.insertZlglZlsb(zlglZlsb);
        insertZlglZlsbFj(zlglZlsb);
        return rows;
    }

    /**
     * 批量新增专利申报
     *
     * @param zlglZlsbs 专利申报List
     * @return 结果
     */
    @Override
    public int batchInsertZlglZlsb(List<ZlglZlsb> zlglZlsbs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(zlglZlsbs)) {
            try {
                for (int i = 0; i < zlglZlsbs.size(); i++) {
                    int row = zlglZlsbMapper.insertZlglZlsb(zlglZlsbs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == zlglZlsbs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改专利申报
     *
     * @param zlglZlsb 专利申报
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZlglZlsb(ZlglZlsb zlglZlsb) {
        zlglZlsb.setUpdateTime(DateUtils.getNowDate());
//        zlglZlsbMapper.deleteZlglZlsbFjByZlsbId(zlglZlsb.getZlsbId());
        insertZlglZlsbFj(zlglZlsb);
        return zlglZlsbMapper.updateZlglZlsb(zlglZlsb);
    }

    /**
     * 批量删除专利申报
     *
     * @param zlsbIds 需要删除的专利申报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZlglZlsbByZlsbIds(String[] zlsbIds) {
        zlglZlsbMapper.deleteZlglZlsbFjByZlsbIds(zlsbIds);
        return zlglZlsbMapper.deleteZlglZlsbByZlsbIds(zlsbIds);
    }

    /**
     * 删除专利申报信息
     *
     * @param zlsbId 专利申报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZlglZlsbByZlsbId(String zlsbId) {
        zlglZlsbMapper.deleteZlglZlsbFjByZlsbId(zlsbId);
        return zlglZlsbMapper.deleteZlglZlsbByZlsbId(zlsbId);
    }

    /**
     * 新增附件
     *
     * @param zlglZlsbFj
     * @return
     */
    @Override
    public int insertZlsbFjByZlsbId(ZlglZlsbFj zlglZlsbFj) {
        return zlglZlsbMapper.insertZlsbFjByZlsbId(zlglZlsbFj);
    }

    /**
     * 查询附件列表
     *
     * @param zlglZlsbFj
     * @return
     */
    @Override
    public List<ZlglZlsbFj> selectZlsbFjList(ZlglZlsbFj zlglZlsbFj) {
        return zlglZlsbMapper.selectZlsbFjList(zlglZlsbFj);
    }

    /**
     * 删除专利附件
     *
     * @param fjIds
     * @return
     */
    @Override
    public int deleteZlsbFjByFjIds(Integer[] fjIds) {
        return zlglZlsbMapper.deleteZlsbFjByFjIds(fjIds);
    }

    /**
     * 查询专利流程状态为
     * 审批通过  国知产权局申请 知产申请驳回  授权(正常) 授权(失效) 的数据
     * 用于授权登记页面
     *
     * @param zlglZlsb
     * @return
     */
    @Override
    public List<ZlglZlsb> selectZlBySqDj(ZlglZlsb zlglZlsb) {
        return zlglZlsbMapper.selectZlBySqDj(zlglZlsb);
    }

    /**
     * 专利授权趋势
     * @return
     */
    @Override
    public List<ZlSqVo> getSqQs() {
        return zlglZlsbMapper.getSqQs();
    }

    /**
     * 查询专利流程状态为授权(正常)的专利
     * @param zlglZlsb
     * @return
     */
    @Override
    public List<ZlglZlsb> selectZlListBySq(ZlglZlsb zlglZlsb) {
        return zlglZlsbMapper.selectZlListBySq(zlglZlsb);
    }

    /**
     * 新增专利申报附件信息
     *
     * @param zlglZlsb 专利申报对象
     */
    public void insertZlglZlsbFj(ZlglZlsb zlglZlsb) {
        List<ZlglZlsbFj> zlglZlsbFjList = zlglZlsb.getZlglZlsbFjList();
        String zlsbId = zlglZlsb.getZlsbId();
        if (StringUtils.isNotNull(zlglZlsbFjList)) {
            List<ZlglZlsbFj> list = new ArrayList<ZlglZlsbFj>();
            for (ZlglZlsbFj zlglZlsbFj : zlglZlsbFjList) {
                zlglZlsbFj.setZlsbId(zlsbId);
                list.add(zlglZlsbFj);
            }
//            if (list.size() > 0) {
//                zlglZlsbMapper.batchZlglZlsbFj(list);
//            }
        }
    }
}
